package com.example.gaoyang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gaoyang.domain.Station;
import com.example.gaoyang.domain.request.StationRequest;
import com.example.gaoyang.domain.vo.StationVO;
import com.example.gaoyang.mapper.StationMapper;
import com.example.gaoyang.service.StationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@Transactional
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {

    @Override
    public void addStation(StationRequest stationRequest) {
        Station station = BeanUtil.copyProperties(stationRequest, Station.class);
        this.save(station);
    }

    @Override
    public List<StationVO> getStationList(StationRequest stationRequest) {
        LambdaQueryWrapper<Station> wrapper = this.getQueryWrapper(stationRequest);
        List<StationVO> list = BeanUtil.copyToList(this.list(wrapper), StationVO.class);
        return list;
    }

    @Override
    public void removeStation(StationRequest stationRequest) {
        this.removeById(stationRequest.getId());
    }

    @Override
    public void updateStation(StationRequest stationRequest) {
        Station station = BeanUtil.copyProperties(stationRequest, Station.class);
        this.updateById(station);
    }

    private LambdaQueryWrapper<Station> getQueryWrapper(StationRequest stationRequest) {
        return new LambdaQueryWrapper<Station>()
                .eq(StringUtils.isNotEmpty(stationRequest.getId()), Station::getId, stationRequest.getId())
                .like(StringUtils.isNotEmpty(stationRequest.getStationName()), Station::getStationName, stationRequest.getStationName())
                .eq(StringUtils.isNotEmpty(stationRequest.getWayId()), Station::getWayId, stationRequest.getWayId());
    }

}
